import os

from face_matcher import match_multiple_faces

from utils.logger_config import logger


def main():
    # 源图片目录路径
    source_directory = "static/images/src-face/chenxun"
    # 目标目录路径
    target_directory = "static/images/tar-face/chenxun"
    # 模型存储目录
    # model_directory = "./custom_models"

    # 获取目标目录中的所有图片数量
    target_images = [
        f
        for f in os.listdir(target_directory)
        if f.lower().endswith((".jpg", ".jpeg", ".png"))
    ]
    total_target_images = len(target_images)

    logger.info("\n" + "=" * 50)
    logger.info("多图匹配模式")
    logger.info("=" * 50)
    logger.info(
        f"开始匹配源目录 {source_directory} 中的图片与目标目录 {target_directory} 中的图片..."
    )

    # 调用多源图片匹配函数
    match_results = match_multiple_faces(source_directory, target_directory)

    # 输出多图匹配结果
    total_matched = 0
    all_matched_images = set()

    logger.info("\n匹配结果汇总:")
    for source_img, matched_images in match_results.items():
        logger.info(f"\n源图片 {source_img} 匹配成功的图片:")
        if matched_images:
            # 按匹配率从高到低排序
            sorted_matches = sorted(matched_images, key=lambda x: x[1], reverse=True)
            for img, match_rate in sorted_matches:
                logger.info(f"- {img} (匹配率: {match_rate:.2f}%)")
                all_matched_images.add(img)
            logger.info(f"共找到 {len(matched_images)} 张匹配图片")
            total_matched += len(matched_images)
        else:
            logger.info("未找到匹配的图片")

    multi_match_count = len(all_matched_images)
    multi_match_rate = (multi_match_count / total_target_images) * 100
    logger.info(
        f"\n多图匹配总计匹配成功 {multi_match_count} 张不同图片，匹配率: {multi_match_rate:.2f}%"
    )


if __name__ == "__main__":
    main()
